﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Entities.MetaDataSchema;
using System.Text.RegularExpressions;

namespace Common.Entities
{
    public abstract class Entity: ICloneable
    {
        #region Properties
        private string _id = "";
        public string Id
        {
            set
            {
                _id = value;
            }
            get 
            { 
                if(string.IsNullOrEmpty(_id))
                    _id = Guid.NewGuid().ToString();
                return _id; 
            }
        }
        private string _name = "";
        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }
        private bool _entitySelected = false;
        public bool EntitySelected
        {
            get { return _entitySelected; }
            set
            {
                _entitySelected = value;
            }
        }

        protected Entity _Parent;
        public virtual Entity Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
        #endregion
        public Entity()
        {

            //_id = Guid.NewGuid().ToString();
        }


        public string GetGuid(string value)
        {
            string result = value;
            string guidMatch = @"(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})";
            Regex internalReg = new Regex(guidMatch, RegexOptions.Singleline);
            Match internalMatch = internalReg.Match(value);
            if(internalMatch.Success)
                result = internalMatch.Value;
            return result;
        }
        public void ConvertToXML(string path)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());
            System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
            x.Serialize(writer, this);

        }
        public virtual void UnSelect()
        {
            EntitySelected = false;
        }
        public virtual void Select()
        {
            EntitySelected = true;
            if (Parent != null)
                Parent.Select();
        }
        public Entities.Entity ConvertToObject(string path)
        {
            Entities.Entity result = null;
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());
            System.IO.StreamReader reader = new System.IO.StreamReader(path);
            result = x.Deserialize(reader) as Entities.Entity;
            return result;
        }


        public static Entity GetbyId(string id, List<Entities.Entity> entities)
        {
            foreach (Entity e in entities)
            {
                if (e.Id == id)
                    return e;
            }
            return null;
        }
        public static Database GetbyId(string id, List<Entities.MetaDataSchema.Database> databases)
        {
            foreach (MetaDataSchema.Database d in databases)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        public static Function GetbyId(string id, List<Entities.MetaDataSchema.Function> functions)
        {
            foreach (MetaDataSchema.Function d in functions)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        public static StoredProcedure GetbyId(string id, List<Entities.MetaDataSchema.StoredProcedure> storedProcedures)
        {
            foreach (MetaDataSchema.StoredProcedure d in storedProcedures)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        public static Table GetbyId(string id, List<Entities.MetaDataSchema.Table> tables)
        {
            foreach (MetaDataSchema.Table d in tables)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        public static View GetbyId(string id, List<Entities.MetaDataSchema.View> views)
        {
            foreach (MetaDataSchema.View d in views)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        public static Column GetbyId(string id, List<Entities.MetaDataSchema.Column> columns)
        {
            foreach (MetaDataSchema.Column d in columns)
            {
                if (d.Id == id)
                    return d;
            }
            return null;
        }
        #region ICloneable Members

        public virtual object Clone()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        
        #endregion
    }


}
